package com.ggchzzz;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class Application {
    public static void main(String[] args) throws IOException {
        Map<String,String> rootList=getUrlList();
        getZhangjie(rootList);
    }
    public static  void getZhangjie(Map<String,String> map) throws IOException {
        String sepa = java.io.File.separator;
        System.out.println("sepa\t"+sepa);
        Document document = null;
        Map<String,String> DetailMap=new LinkedHashMap<String,String>();
        for (Map.Entry<String,String> entry:map.entrySet()){
            String title=entry.getKey();
            String url=entry.getValue();
            System.out.print("\t");
            System.out.print("小说名字\t"+title+"\t");
            System.out.print("小说链接\t"+url);
            System.out.print("\n");
            String originPath="resources"+sepa+title;
            File fileD=new File(originPath);
            if(!fileD.exists()){
                System.out.println("创建小说目录\t"+fileD.mkdirs());
            }
            try {
                document = Jsoup
                        .connect(
                                url)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Element element = document.body();
            Elements dds=element.select("dd>a");
            int size=dds.size();
            for (int i = 0; i < size; i++) {
                System.out.println(dds.get(i).text()+"\t"+url+dds.get(i).attr("href"));
                String filePath=originPath+sepa+dds.get(i).text()+".txt";
                File file=new File(filePath.replaceAll("[/?|/*]",""));
                if(!file.exists())
                {
                    try {
                        System.out.println("文件创建结果\t"+filePath+"\t"+file.createNewFile());
                    }
                    catch (Exception e){
                        filePath="第"+i+"章";
                        System.out.println("文件创建结果\t"+filePath+"\t"+file.createNewFile());
                        System.out.println(e);
                    }
                }
                DetailMap.put(filePath,url+dds.get(i).attr("href"));
            }
        }
        writeTofile(DetailMap);

    }
    public static void writeTofile(Map<String,String> map) throws IOException {
        Document document = null;
        for (Map.Entry<String,String> entry:map.entrySet()){
            String path=entry.getKey();//path
            System.out.println("path\t"+path);
            String url=entry.getValue();//href
            System.out.println("小说内容是\t"+url);
            try {
                document = Jsoup
                        .connect(
                                url)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Element element=document.body();
            Elements elements=element.select("td>p");
            System.out.println("小说章节内容是\t"+elements.text());
            File file=new File(path.replaceAll("[/?|/*]",""));
            FileWriter fileWritter = new FileWriter(file,false);
            fileWritter.write(elements.text());
            fileWritter.close();
        }
    }
    public  static Map<String,String> getUrlList(){
        Document document = null;
        int  page=0;
        String rootUrl="https://wx.tianyabooks.com";
        String startUrl="https://wx.tianyabooks.com/book/";
        Map<String,String> UrlList=new LinkedHashMap();
        try {
            document = Jsoup
                    .connect(
                            startUrl)
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element element = document.body();
        Elements ths = element.select("div>li>a");
        int size=ths.size();
        System.out.println("小说数量"+size);
        for (int i = 0; i < size; i++) {
            UrlList.put(ths.get(i).text(),rootUrl+ths.get(i).attr("href"));
        }
        return  UrlList;
    }
}
